package com.ruoyi.wms.controller.stock;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.idempotent.annotation.RepeatSubmit;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.common.web.core.BaseController;
import com.ruoyi.system.domain.vo.SysUserVo;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.wms.common.enu.PurchaseStatusEnum;
import com.ruoyi.wms.domain.bo.stock.PurchaseRequestsBo;
import com.ruoyi.wms.domain.vo.PurchaseRequestsVo;
import com.ruoyi.wms.domain.vo.PurchaseStatisticsVo;
import com.ruoyi.wms.domain.vo.UserVo;
import com.ruoyi.wms.service.PurchaseRequestsService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 采购申请
 *
 * @author 邓滔滔
 * @date 2024-11-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wms/purchaseRequests")
public class PurchaseRequestsController extends BaseController {

    private final PurchaseRequestsService purchaseRequestsService;

    private final SysUserMapper sysUserMapper;

    /**
     * 查询采购申请列表
     */
    @SaCheckPermission("wms:purchaseRequests:all")
    @GetMapping("/list")
    public TableDataInfo<PurchaseRequestsVo> list(PurchaseRequestsBo bo, PageQuery pageQuery) {
        return purchaseRequestsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出采购申请列表
     */
    @SaCheckPermission("wms:purchaseRequests:all")
    @Log(title = "采购申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PurchaseRequestsBo bo, HttpServletResponse response) {
        List<PurchaseRequestsVo> list = purchaseRequestsService.queryList(bo);
        ExcelUtil.exportExcel(list, "采购申请", PurchaseRequestsVo.class, response);
    }

    /**
     * 获取采购申请详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("wms:purchaseRequests:all")
    @GetMapping("/{id}")
    public R<PurchaseRequestsVo> getInfo(@NotNull(message = "主键不能为空")
                                         @PathVariable Long id) {
        return R.ok(purchaseRequestsService.queryById(id));
    }

    /**
     * 获取所有用户信息
     *
     * @return
     */
    @SaCheckPermission("wms:purchaseRequests:all")
    @GetMapping("/listAll")
    public R<List<UserVo>> getUserByUserName() {

        List<SysUserVo> sysUsers = sysUserMapper.selectVoList();
        List<UserVo> userVos = BeanUtil.copyToList(sysUsers, UserVo.class);
        System.out.println(sysUsers);
        return R.ok(userVos);
    }

    /**
     * 暂存采购申请
     */
    @SaCheckPermission("wms:purchaseRequests:all")
    @Log(title = "采购申请", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PurchaseRequestsBo bo) {
        //修改采购状态为暂存
        bo.setPurchaseStatus(PurchaseStatusEnum.WAIT.getValue());
        purchaseRequestsService.insertByBo(bo);
        return R.ok();
    }


    /**
     * 审核采购申请接口
     */
    @SaCheckPermission("wms:purchaseRequests:all")
    @Log(title = "采购申请", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/audit")
    public R<Boolean> audit(@RequestParam Long id) {
        Boolean audited = purchaseRequestsService.audit(id);
        return R.ok(audited);
    }

    /**
     * 采购申请入库
     */
    @SaCheckPermission("wms:purchaseRequests:all")
    @Log(title = "采购申请", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/inbound")
    public R<Boolean> inbound(@RequestParam Long id) {
        Boolean audited = purchaseRequestsService.inbound(id);
        return R.ok(audited);
    }

    /**
     * 修改采购申请
     */
    @SaCheckPermission("wms:purchaseRequests:all")
    @Log(title = "采购申请", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PurchaseRequestsBo bo) {
        purchaseRequestsService.updateByBo(bo);
        return R.ok();
    }

    /**
     * 删除采购申请
     *
     * @param id 主键串
     */
    @SaCheckPermission("wms:purchaseRequests:all")
    @Log(title = "采购申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> remove(@PathVariable Long id) {
        purchaseRequestsService.deleteById(id);
        return R.ok();
    }


    /**
     * 采购订单汇总接口-接收开始时间，结束时间日期对象
     */
    @SaCheckPermission("wms:purchaseRequests:all")
    @GetMapping("/statistics")
    public R<PurchaseStatisticsVo> statistics(@DateTimeFormat(pattern = "yyyy-MM-dd") String startTime,
                                              @DateTimeFormat(pattern = "yyyy-MM-dd") String endTime) {

        PurchaseStatisticsVo statistics = purchaseRequestsService.statistics(startTime, endTime);
        return R.ok(statistics);

    }
}
